<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="案例标题" prop="noticeTitle">
        <el-input
          v-model="queryParams.data.name"
          placeholder="请输入案例标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="noticeList"
    >
      <el-table-column
        label="ID"
        align="center"
        prop="componentInfoConfListId"
        width="100"
      />
      <el-table-column
        label="案例封面"
        align="center"
        prop="backgroundUrl"
        :show-overflow-tooltip="true"
      >
	<template slot-scope="scope">
  	<el-popover placement="top-start" trigger="hover">
    	<img :src="scope.row.backgroundUrl" alt="" style="width: 150px;height: 150px">
      <img slot="reference" :src="scope.row.backgroundUrl" style="width: 80px;height: 80px">
    </el-popover>
  </template>
</el-table-column>
      <el-table-column label="案例标题" align="center" prop="name" width="100">
      </el-table-column>
      <el-table-column
        label="案例描述"
        align="center"
        prop="description"
        width="100"
      >
      </el-table-column>
      <el-table-column
        label="浏览量"
        align="center"
        prop="createBy"
        width="100"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="案例封面" prop="backgroundUrl">
          <upload-view
            v-model="form.backgroundUrl"
            @img="getimgListback"
          ></upload-view>
        </el-form-item>
        <el-form-item label="案例标题" prop="name">
          <el-input v-model="form.name" placeholder="请输入公告标题" />
        </el-form-item>
        <el-form-item label="案例描述">
          <el-input v-model="form.description" placeholder="请输入公告标题" />
        </el-form-item>
        <el-form-item label="案例详情">
          <editor v-model="form.particulars" :min-height="192" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import uploadView from "../../components/ImageUpload";
import {
  componentlist,
  getNotice,
  delNoticeupdata,
  addproduct,
  updateNotice,
  updataProduct
} from "@/api/system/notice";

export default {
  name: "Notice",
  dicts: ["sys_notice_status", "sys_notice_type"],
  data() {
    return {
        dateRange:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        data: {
          startTime: "",
          endTime: "",
          name: "",
          type: 2,
        },
      },
      // 表单参数
      form: {
        type: "2",
        name: "",
        description: "",
        particulars: "",
        backgroundUrl: "",
      },
      // 表单校验
      rules: {
        backgroundUrl: [
          {
            required: true,
            message: "案例封面不能为空",
            trigger: "change,blur",
          },
        ],
        name: [
          { required: true, message: "案例标题不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "案例描述不能为空", trigger: "blur" },
        ],
        particulars: [
          { required: true, message: "案例详情不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  components: {
    uploadView,
  },
  methods: {
    getimgListback(data) {
      console.log("图片路径back", data);
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      this.queryParams.data.startTime = this.dateRange[0]
      this.queryParams.data.endTime = this.dateRange[1]
      componentlist(this.queryParams, this.queryParams.data).then(
        (response) => {
          this.noticeList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.dateRange=[]
       this.queryParams.data.name = ''
      this.resetForm("queryForm");
      this.handleQuery();

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const noticeId = row.componentInfoConfListId;
      addproduct(noticeId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改行业";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
          if (this.form.componentInfoConfListId) {
            updataProduct(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.form = {}
              this.getList();
            });
          } else {
            console.log(this.form);
            this.form.type = "2";
            updataProduct(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.form = {}
              this.getList();
            });
          }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.componentInfoConfListId || this.ids;
      this.$modal
        .confirm('是否确认删除编号为"' + noticeIds + '"的数据项？')
        .then(function () {
          return delNoticeupdata(noticeIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
